package com.atguigu.datastream.test.day03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink_04_Source_kafka_new
 * Package: com.atguigu.test.day03
 * Description:
 *             从kafka读取数据  新版本  1。14 以后
 *
 * @Author ChenJun
 * @Create 2023/4/9 19:24
 * @Version 1.0
 */
public class Flink_04_Source_kafka_new {
    public static void main(String[] args) throws Exception {

        //1. 创建Flink 流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //2. 设置并行度
        env.setParallelism(1);

        // TODO 3. congkafka读取数据
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("hadoop102:9092")
                .setGroupId("221109")
                .setTopics("first")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        DataStreamSource<String> streamSource = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafkaSource");

        //4. 打印
        streamSource.print();

        //5. 执行
        env.execute();
    }
}
